package com.bawei.module_inspection

import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.module_inspection.databinding.ActivityInspectionAddBinding
import com.bawei.module_inspection.model.PatrolModelItem
import com.bawei.module_inspection.ui.InspectionViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = Router.Ui.InspectionAddActivity)
class InspectionAddActivity : BaseActivity<InspectionViewModel, ActivityInspectionAddBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_inspection_add
    override val mViewModel: InspectionViewModel by viewModels()
    override fun initView() {
    }

    override fun initListener() {
        mBinding.ivPointImage.setOnClickListener {
            getContent.launch("image/*")
        }
    }

    override fun initData() {
        mBinding.inspectionButton.setOnClickListener {
            val patrolModelItem = collectPatrolData()
            mViewModel.postInspection(patrolModelItem)
            lifecycleScope.launch {
                mViewModel.mInspectionState.collect{
                    when (it) {
                        Empty -> {
                            showToast("添加失败")
                        }
                        is Failed -> {
                            showToast("添加失败")
                        }
                        Loading -> {

                        }
                        Success -> {
                            showToast("添加成功")
                            finish()
                        }
                    }

                }
            }
        }
    }

    private val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri : Uri? ->
        uri?.let {
            mBinding.ivPointImage.setImageURI(uri)
            selectImage(uri)
        }
    }

    private fun selectImage(uri:Uri) {
        val filePath = uri.path
    }

    private fun collectPatrolData() : PatrolModelItem {
        val projectId = mBinding.spinnerBelongProject.selectedItem?.toString() ?: ""
        val patrolType = mBinding.spinnerInspectionType.selectedItem?.toString() ?: ""
        val pointName = mBinding.etPointName.text.toString()
        val pointLocation = mBinding.etPointLocation.text.toString()
        val patrolStandard = mBinding.spinnerInspectionStandard.selectedItem?.toString() ?: ""
        val pointImages = mBinding.ivPointImage.toString()

        return PatrolModelItem(
            id = 0,
            projectId = projectId,
            patrolType = patrolType,
            pointName = pointName,
            pointLocation = pointLocation,
            patrolStandard = patrolStandard,
            pointImages = pointImages,
            pointStatus = "启用"
        )
    }
}